Imports Microsoft
Imports Microsoft.Win32
Imports Microsoft.Win32.Registry
Imports System.Collections
Imports System.Windows.Forms
Imports System.IO
Imports System.Threading

' Returns the sum of the files in the folder.
    ' dPath: Path of the directory
    ' include subfolders: set if include subfolders ;)
    Public abort As Boolean
    Function GetFolderSize(ByVal DirPath As String, ByVal includeSubFolders As Boolean) As Long
        Try
            Dim size As Long = 0
            Dim diBase As New DirectoryInfo(DirPath)
            Dim files() As FileInfo
            If includeSubFolders Then
                files = diBase.GetFiles("*", SearchOption.AllDirectories)

            Else
                files = diBase.GetFiles("*", SearchOption.TopDirectoryOnly)
            End If
            Dim ie As IEnumerator = files.GetEnumerator
            While ie.MoveNext And Not abort

                size += DirectCast(ie.Current, FileInfo).Length
            End While
            Return size
        Catch ex As Exception
            MsgBox("Error: " & ex.Message)
            Return -1
        End Try
    End Function